package com.mars.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.mars.common.enums.BusinessType;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.UserBrowseHistory;
import com.mars.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.mars.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.service.IUserBrowseHistoryService;
import com.mars.module.admin.request.UserBrowseHistoryRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 用户浏览历史控制层
 *
 * @author mars
 * @date 2024-03-04
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "3.用户浏览历史接口管理",tags = "3.用户浏览历史接口管理")
@RequestMapping("/admin/userBrowseHistory" )
public class UserBrowseHistoryController {

    private final IUserBrowseHistoryService iUserBrowseHistoryService;

    /**
     * 分页查询用户浏览历史列表
     */
    @ApiOperation(value = "分页查询用户浏览历史列表")
    @PostMapping("/pageList")
    public R<PageInfo<UserBrowseHistory>> pageList(@RequestBody UserBrowseHistoryRequest userBrowseHistoryRequest) {
        return R.success(iUserBrowseHistoryService.pageList(userBrowseHistoryRequest));
    }

    /**
     * 获取用户浏览历史详细信息
     */
    @ApiOperation(value = "获取用户浏览历史详细信息")
    @GetMapping(value = "/query/{id}")
    public R<UserBrowseHistory> detail(@PathVariable("id") Long id) {
        return R.success(iUserBrowseHistoryService.getById(id));
    }

    /**
     * 新增用户浏览历史
     */
    @Log(title = "新增用户浏览历史", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增用户浏览历史")
    @PostMapping("/add")
    public R<Void> add(@RequestBody UserBrowseHistoryRequest userBrowseHistoryRequest) {
        iUserBrowseHistoryService.add(userBrowseHistoryRequest);
        return R.success();
    }

    /**
     * 修改用户浏览历史
     */
    @Log(title = "修改用户浏览历史", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户浏览历史")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody UserBrowseHistoryRequest userBrowseHistoryRequest) {
        iUserBrowseHistoryService.update(userBrowseHistoryRequest);
        return R.success();
    }

    /**
     * 删除用户浏览历史
     */
    @Log(title = "删除用户浏览历史", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除用户浏览历史")
    @PostMapping("/delete")
    public R<Void> remove(@RequestParam(value = "newsId") String newsId, @RequestParam(value = "userId") String userId) {
        iUserBrowseHistoryService.remove(newsId, userId);
        return R.success();
    }

    /**
    * 导出用户浏览历史
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody UserBrowseHistoryRequest userBrowseHistoryRequest) throws IOException {
        List<UserBrowseHistory> list = iUserBrowseHistoryService.list(userBrowseHistoryRequest);
        ExcelUtils.exportExcel(list, UserBrowseHistory.class, "用户浏览历史信息", response);
    }
}
